//
//  SongListViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/5/3.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "SongListViewController.h"
#import "SongListCollectionViewCell.h"
#import "songListModel.h"
#import "SongListDetailViewController.h"
#import "SongListTypeViewController.h"
@interface SongListViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, retain) YHYBaseCollectionView *collectionView;

@property (nonatomic, retain) YHYBaseButton *allButton;

@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation SongListViewController

-(void)dealloc {
    
    [_type release];
    [_flowLayout release];
    [_allButton release];
    [_dataSource release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"歌单";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    self.dataSource = [NSMutableArray array];
    
    [self creatView];
    [self dataHandle];
    
}

- (void)dataHandle {
    NSString *url =  @"http://search.dongting.com/songlist/search?s=s200&size=10&hid=6518303829743017&utdid=VfjPsN6N7esDAFG9M0qbxE1S&q=tag%3A最热&net=2&app=ttpod&v=v8.4.1.2016032512&alf=alf702006&tid=0&uid=867992029051448&f=f3030&page=1&imsi=460025418172304";
    [YHYAFNetTool GETNetWirhUrl:url body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            SongListModel *songListModel = [[SongListModel alloc] init];
            [songListModel setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:songListModel];
        }
        [_collectionView reloadData];
    } Failure:^(NSError *error) {
        
    }];
    
}

- (void)creatView {
    
    YHYBaseView *topView =[[YHYBaseView alloc] initWithFrame:CGRectMake(0, 10, WIDTH, 40)];
    topView.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
    [self.view addSubview:topView];
    YHYBaseButton *button = [[YHYBaseButton alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 0.3, topView.frame.size.height)];
    [button setTitle:self.type forState:UIControlStateNormal];
    [button setTintColor:[UIColor colorWithRed:0.57 green:0.59 blue:0.62 alpha:1.00]];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:button];
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.itemSize = CGSizeMake(WIDTH / 2, WIDTH / 2);
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[YHYBaseCollectionView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT - 164) collectionViewLayout:_flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.bounces = NO;
    [_collectionView registerClass:[SongListCollectionViewCell class] forCellWithReuseIdentifier:@"SongListCollectionViewCellIdentifier"];
    [self.view addSubview:_collectionView];
    
}

- (void)buttonAction:(UIButton *)sender {
    
    SongListTypeViewController *typeVC = [[SongListTypeViewController alloc] init];
    
    typeVC.blockOfButtonType = ^(NSString *type){
        
        if (![self.type isEqualToString:type]) {
            [_allButton setTitle:type forState:UIControlStateNormal];
            
            [YHYAFNetTool GETNetWirhUrl:[NSString stringWithFormat:@"http://search.dongting.com/songlist/search?size=10&q=tag:最新|%@&page=1", type] body:nil headerFile:nil response:YHYJSON Success:^(id result) {
                
                [_dataSource removeAllObjects];
                for (NSDictionary *dic in result[@"data"]) {
                    SongListModel *songListModel = [[SongListModel alloc] init];
                    [songListModel setValuesForKeysWithDictionary:dic];
                    [_dataSource addObject:songListModel];
                }
                [_collectionView reloadData];

                
            } Failure:^(NSError *error) {
                
            }];
        }
        
    };
    
    [self.navigationController pushViewController:typeVC animated:YES];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SongListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongListCollectionViewCellIdentifier" forIndexPath:indexPath];
    cell.singListButtonModel = _dataSource[indexPath.item];
    cell.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SongListDetailViewController *detailVC = [[SongListDetailViewController alloc] init];
    SongListModel *songListModel = _dataSource[indexPath.item];
    detailVC.songListId = [NSString stringWithFormat:@"%@", songListModel.quan_id];
    detailVC.imageStr = songListModel.pic_url;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
